<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'

//定义数据模型 searchEmp
const searchEmp = ref({ name: '', gender: '', job: '' })
//定义数据模型 empList
const empList = ref([])

//定义函数 search
async function search() {
    console.log(`接收到了参数: ${searchEmp.value.name} ${searchEmp.value.gender} ${searchEmp.value.job}`);
    const result = await axios.get(`https://web-server.itheima.net/emps/list?name=${searchEmp.value.name}&gender=${searchEmp.value.gender}&job=${searchEmp.value.job}`);
    empList.value = result.data.data;
}

//定义函数 clear
function clear() {
    searchEmp.value = { name: '', gender: '', job: '' }
    search()
}

//定义钩子函数
onMounted(() => {
    search()
})
</script>

<template>
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="searchEmp" class="demo-form-inline">
        <el-form-item label="姓名">
            <el-input v-model="searchEmp.name" placeholder="请输入姓名" />
        </el-form-item>

        <el-form-item label="性别">
            <el-select v-model="searchEmp.gender" placeholder="请选择">
                <el-option label="男" value="1" />
                <el-option label="女" value="2" />
            </el-select>
        </el-form-item>

        <el-form-item label="职位">
            <el-select v-model="searchEmp.job" placeholder="请选择">
                <el-option label="班主任" value="1" />
                <el-option label="讲师" value="2" />
                <el-option label="其他" value="3" />
            </el-select>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="clear">清空</el-button>
        </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="empList" border style="width: 80%">
        <el-table-column prop="id" label="ID" width="80" align="center" />
        <el-table-column prop="name" label="姓名" width="150" align="center" />
        <el-table-column label="头像" width="150" align="center">
            <template #default="scope">
                <img :src="scope.row.image" alt="" style="width: 40px;height: 40px;">
            </template>
        </el-table-column>
        <el-table-column label="性别" width="100" align="center">
            <template #default="scope">
                    {{ scope.row.gender == 1 ? '男' : '女' }}
                </template>
        </el-table-column>
        <el-table-column label="职位" width="150" align="center">
            <template #default="scope">
                <span v-if="scope.row.job == 1">班主任</span>
                <span v-else-if="scope.row.job == 2">讲师</span>
                <span v-else>其他</span>
            </template>
        </el-table-column>
        <el-table-column prop="entrydate" label="入职日期" width="200" align="center" />
        <el-table-column prop="updatetime" label="更新时间" align="center" />
    </el-table>

</template>

<style scoped></style>
